﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using CCF20Model;
using System.ComponentModel;

namespace CCF20
{
    /// <summary>
    /// Interaction logic for ContactCustomer.xaml
    /// </summary>
    public partial class ContactCustomer : Window
    {
        private Vehicle vehicle;
        private VehicleModel vehicleModel;
        private CustomizeModel customizeModel;
        public IList<ContactHistory> contactList { get; set; }
        public IList<SMSHistory> smsList { get; set; }
        CCF20Entities ccf20Ent;
        
      
        public ContactCustomer(VehicleModel vmm, Vehicle vr)
        {
            InitializeComponent();
            vehicle = vr;
            vehicleModel = vmm;
            customizeModel = new CustomizeModel();
            ccf20Ent = new CCF20Entities();
            
        }

        private void Contact_Click(object sender, RoutedEventArgs e)
        {
            
            string strAction = ((Button)sender).Content.ToString();
            if (strAction == "提交电话追踪结果")
            {
                ContactHistory contactHistory = new ContactHistory();
                contactHistory.ContactResult = this.ContactResult.SelectedValue.ToString();
                contactHistory.IsActive = true;
                contactHistory.Remark = this.ContactMemo.Text;
                contactHistory.ContactTime = DateTime.Now;
                contactHistory.BookTime = this.BookTime.SelectedDate;
                vehicle.LastContactResult = this.ContactResult.SelectedValue.ToString();
                vehicle.LastContactTime = DateTime.Now;
                contactHistory.Vehicle = vehicle;
                contactHistory.Operator = (string)Application.Current.Properties["CurrentUser"];
                vehicleModel.AddContactHistoryForVehicle(vehicle, contactHistory);
                MessageBox.Show("保存成功");
                contactList.Add(contactHistory);
                
            }
            else if (strAction == "发送短信")
            {

                if (SMSTemplate.SelectedItem == null)
                {
                    return;
                }
                SMSHistory smsHistory = new SMSHistory();
                smsHistory.VehicleID = vehicle.VehicleID;
                smsHistory.IsActive = true;
                smsHistory.QueueTime = DateTime.Now;
                smsHistory.SMSTemplateID = ((CCF20Model.SMSTemplate)this.SMSTemplate.SelectedItem).SMSTemplateID;
                smsHistory.SMSText = this.SMSTxt.Text;
                smsHistory.CustomerName = vehicle.Customer.Name;
                smsHistory.CustomerMobile = vehicle.Customer.FirstMobile;
                smsHistory.Operator = (string)Application.Current.Properties["CurrentUser"]; 
                vehicleModel.AddSMSHistoryForVehicle(vehicle, smsHistory);
                MessageBox.Show("您的短信已经进入发送队列");
                smsList.Add(smsHistory);

                //CCFZIC ccfZIC = new CCFZIC();

                
            }
            this.Close();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            //this.ContactResult.ItemsSource = customizeModel.GetContactResult();
            this.SMSTemplate.ItemsSource = customizeModel.GetSMSTemplate();
            this.BookTime.Visibility = System.Windows.Visibility.Hidden;
            DependencyPropertyDescriptor dpd = DependencyPropertyDescriptor.FromProperty(ComboBox.TextProperty, typeof(ComboBox));

            dpd.AddValueChanged(ContactResult, OnTextChanged);
 

            
        }
        private void OnTextChanged(object sender, EventArgs args)
        {
            if (this.ContactResult.Text.Contains("同意回场"))
            {
                this.BookTime.Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                this.BookTime.Visibility = System.Windows.Visibility.Hidden;
            }
        }

        private void SMSTemplate_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count==0) return;
            CCF20Model.SMSTemplate myTemplate = (CCF20Model.SMSTemplate)e.AddedItems[0];
            string smstext = myTemplate.SMSText.Replace("#lxr#",vehicle.Customer.Name)
                                               .Replace("#NowLs#", vehicle.CurrentMileage.ToString())
                                               .Replace("#CompanyName#",ccf20Ent.CompanyProfiles.FirstOrDefault().CompanyName)
                                               .Replace("#CompanyAddress#",ccf20Ent.CompanyProfiles.FirstOrDefault().CompanyAddress)
                                               .Replace("#CompanyPhone#",ccf20Ent.CompanyProfiles.FirstOrDefault().Telephone);
            this.SMSTxt.Text = smstext;
                                               

        } 

        
    }
}
